﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class application_step3 : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        InitializeUI();
        LoadAirports();
        LoadCraftTypes(langId);
        LoadLevelOfImportanceTypes(langId);
        LoadPlacementTypes(langId);
        LoadPackingTypes(langId);
        LoadSpecialCondTypes(langId);
        setAttributes();
        LoadDimensionTypes(langId);
        LoadVolumeTypes(langId);
        LoadWeightTypes(langId);
        LoadCurrencyTypes(langId);
    }

    private void LoadDimensionTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 18);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlHeight.Items.Clear();
                ddlLenght.Items.Clear();
                ddlWidth.Items.Clear();
                while (dr.Read())
                {
                    ddlHeight.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                    ddlLenght.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                    ddlWidth.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadVolumeTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 19);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlVolume.Items.Clear();

                while (dr.Read())
                {
                    ddlVolume.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadWeightTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 20);
                
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlWeight.Items.Clear();
                while (dr.Read())
                {
                    ddlWeight.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadCurrencyTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 11);
               
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPrice.Items.Clear();
                while (dr.Read())
                {
                    ddlPrice.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void setAttributes()
    {
        ddlAirportLoading.Attributes.Add("validate", "true");
        ddlCraftTypes.Attributes.Add("validate", "true");
        ddlPlacementTypes.Attributes.Add("validate", "true");
        tbWeight.Attributes.Add("validate", "true");
        string type_offer_id = "0";
        if (Request.QueryString["type_offer_id"] != null)
        {
            type_offer_id = Request.QueryString["type_offer_id"].ToString(); 
        }
        if (type_offer_id!="70")
        {
            ddlPackingTypes.Attributes.Add("validate", "true");
            tbHeight_rfv.Visible = false;
            tbLenght_rfv.Visible = false;
            tbWidth_rfv.Visible = false;
        }
        else
        {
            ddlPackingTypes_rfv.Visible = false;
           tbHeight.Attributes.Add("validate", "true");
           tbLenght.Attributes.Add("validate", "true");
           tbWidth.Attributes.Add("validate", "true");
        }
    }

    private void LoadAirports()
    {
        ddlAirportLoading.Visible = false;
        ddlAirportUnloading.Visible = false;
        lbl_aiport_loading.Visible = false;
        lbl_aiport_unloading.Visible = false;
        /*using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            long country_load_id = 0;
            if (Request.QueryString["country_load_id"] != null)
            {
                Int64.TryParse(Request.QueryString["country_load_id"].ToString(), out country_load_id);
            }

            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetPlacesByCountry(" + country_load_id + ",3) WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlAirportLoading.Items.Clear();
                ddlAirportLoading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlAirportLoading.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }

            long country_unload_id = 0;
            if (Request.QueryString["country_unload_id"] != null)
            {
                Int64.TryParse(Request.QueryString["country_unload_id"].ToString(), out country_unload_id);
            }

            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetPlacesByCountry(" + country_unload_id + ",3) WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();            
                ddlAirportUnloading.Items.Clear();
                ddlAirportUnloading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlAirportUnloading.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }*/
    }

    private void LoadCraftTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,10000,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 1);
               
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlCraftTypes.Items.Clear();
                ddlCraftTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlCraftTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlCraftTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }
    private void LoadLevelOfImportanceTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 16);
               
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlLevelOfImportnce.Items.Clear();
                ddlLevelOfImportnce.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlLevelOfImportnce.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlLevelOfImportnce.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadPlacementTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 8);
              
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPlacementTypes.Items.Clear();
                ddlPlacementTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadPackingTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 6);
                
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPackingTypes.Items.Clear();
                ddlPackingTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlPackingTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlPackingTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadSpecialCondTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 5);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlSpecualConditionTypes.Items.Clear();
                ddlSpecualConditionTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlSpecualConditionTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlSpecualConditionTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
}
